package cn.itliuliu.org.controller;

import cn.itliuliu.basic.util.AjaxResult;
import cn.itliuliu.basic.util.PageList;
import cn.itliuliu.org.domain.Department;
import cn.itliuliu.org.query.DepartmentQuery;
import cn.itliuliu.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

//@GetMapping 简单查询
//@PostMapping 高级查询
//@PutMapping  新增，更新
//@DeleteMapping 删除
//@PatchMapping 批量操作
@RequestMapping("/dept")
@RestController //@Controller + @ResponseBody
@Api(tags = "部门接口")
@Validated
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

//    @RequestMapping(value = "/id",method = RequestMethod.GET)
    @GetMapping("/{id}") // = @RequestMapping(value = "/id",method = RequestMethod.GET)
    @ApiOperation("通过id查询部门")
    public Department queryOne(@PathVariable("id") Long id){
        return departmentService.queryOne(id);
    }

    @GetMapping
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

    @PutMapping
    //contentType = xxx-from    contentType= json
    public AjaxResult addOrUpdate(@RequestBody @Valid Department department){
        if(department.getId() != null){
            departmentService.update(department);
        }else{
            departmentService.add(department);
        }
        return AjaxResult.me();
    }

    @DeleteMapping("/{id}") //xxxx/1
    public AjaxResult delete(@PathVariable("id") Long id){
        departmentService.delete(id);
        return AjaxResult.me();
    }

    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 这里既可以用数组接收也可以用List接收
     * 用什么类型来接收对sql有影响
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        departmentService.patchDelete(ids);
        return AjaxResult.me();
    }

    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }
}
